﻿<% @ LANGUAGE="VBSCRIPT" CODEPAGE="65001" %>
<% Option Explicit %>

<!-- #include file="../adovbs.inc" -->
<!-- #include file="checkfile.asp" -->

<% 
    Dim id, ip
    Dim checkMsg, logMsg

    On Error resume Next

    id = Request.Form("code")
    ip = Request.ServerVariables("REMOTE_ADDR")

    Session.Timeout = 15
    Session("id") = ""

    checkMsg = checkfile(id)

    Response.ContentType="text/plain"
    Response.CharSet = "utf-8"

    If checkMsg Then
        logMsg = insertLog(ip, id, "SUCCESS")
        If logMsg = "SUCCESS" Then
            Session("id") = id
            Response.Write("TRUE")
        Else
            Response.Write(logMsg)
        End If
    Else
        logMsg = insertLog(ip, id, "NOT_FOUND")
        If logMsg = "SUCCESS" Then
            Response.Write("FALSE")
        Else
            Response.Write(logMsg)
        End If
    End If

    Function insertLog(ip, id, status)
        Dim conn, cmd
        Dim dateTime

        On Error resume Next

        dateTime = FormatDateTime(Now(),0)

        Set conn = Server.CreateObject("ADODB.Connection")
        conn.Provider="Microsoft.Jet.OLEDB.4.0"
        conn.Open(Server.MapPath("/")+"\..\database\postcard.mdb")

        Set cmd = Server.CreateObject("ADODB.Command")
        cmd.ActiveConnection = conn
        cmd.CommandText = "INSERT INTO tblSearchLog(IP, SearchCode,Status,SearchDateTime) VALUES(?,?,?,?)"
        cmd.CommandType = adCmdText
        cmd.Parameters.Append cmd.CreateParameter("@ip",adVarChar,adParamInput,50,0)
        cmd.Parameters.Append cmd.CreateParameter("@code",adVarChar,adParamInput,10,0)
        cmd.Parameters.Append cmd.CreateParameter("@status",adVarChar,adParamInput,10,0)
        cmd.Parameters.Append cmd.CreateParameter("@datetime",adVarChar,adParamInput,50,0)
        cmd.Parameters("@ip") = ip
        cmd.Parameters("@code") = id
        cmd.Parameters("@status") = status
        cmd.Parameters("@datetime") = dateTime
        cmd.Execute

        conn.Close

        If Err.number <> 0 Then
            insertLog = "DB_ERROR:" & Err.Source & "(" & Err.number & " - " & Err.Description & ")"
        ELSE
            insertLog = "SUCCESS"
        End If
    End Function
%>

